/**
 * 
 */
package soa.security.test.ws;

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.junit.Test;

/**
 * @author Cre.Gu
 * 
 */
public class DepartmentServiceClient extends WebServiceClient {
	protected static final org.slf4j.Logger log = org.slf4j.LoggerFactory
			.getLogger(DepartmentServiceClient.class);

	public static final String WSURL = BASE_WSDL + "DepartmentService?wsdl";

	public Boolean addDepartment(String organizationId, String department) {
		try {
			String method = "addDepartment";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("department", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.setReturnType(XMLType.XSD_BOOLEAN);

			return (Boolean) call.invoke(new Object[] { organizationId,
					department });
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}

	}

	public Boolean updateDepartment(String organizationId, String department) {
		try {
			String method = "updateDepartment";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("department", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.setReturnType(XMLType.XSD_BOOLEAN);

			return (Boolean) call.invoke(new Object[] { organizationId,
					department });
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}

	}

	public String allDepartments(String organizationId) {
		try {
			String method = "allDepartments";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName);

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.setReturnType(XMLType.XSD_STRING);

			Object obj = call.invoke(new Object[] { organizationId });
			String retData = (String) obj;
			return retData;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public String getDepartment(String organizationId, String departmentId) {
		try {
			String method = "getDepartment";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("departmentId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.setReturnType(XMLType.XSD_STRING);

			return (String) call.invoke(new Object[] { organizationId,
					departmentId });
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}

	public Boolean addPersons(String organizationId, String departmentId,
			String personIds) {
		try {
			String method = "addPersons";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("departmentId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("personIds", XMLType.XSD_STRING, ParameterMode.IN);
			call.setReturnType(XMLType.XSD_BOOLEAN);

			return (Boolean) call.invoke(new Object[] { organizationId,
					departmentId, personIds });
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}

	public Boolean removePersons(String organizationId, String departmentId,
			String personIds) {
		try {
			String method = "removePersons";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("departmentId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("personIds", XMLType.XSD_STRING, ParameterMode.IN);
			call.setReturnType(XMLType.XSD_BOOLEAN);

			return (Boolean) call.invoke(new Object[] { organizationId,
					departmentId, personIds });
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}

	public String allPersons(String organizationId, String departmentId) {
		try {
			String method = "allPersons";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("departmentId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.setReturnType(XMLType.XSD_STRING);

			return (String) call.invoke(new Object[] { organizationId,
					departmentId });
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}

	public Boolean addPositions(String organizationId, String departmentId,
			String positionIds) {
		try {
			String method = "addPositions";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("departmentId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("positionIds", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.setReturnType(XMLType.XSD_BOOLEAN);

			return (Boolean) call.invoke(new Object[] { organizationId,
					departmentId, positionIds });
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}

	public Boolean removePositions(String organizationId, String departmentId,
			String positionIds) {
		try {
			String method = "removePositions";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("departmentId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("positionIds", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.setReturnType(XMLType.XSD_BOOLEAN);

			return (Boolean) call.invoke(new Object[] { organizationId,
					departmentId, positionIds });
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}

	public String allPositions(String organizationId, String departmentId) {
		try {
			String method = "allPositions";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("departmentId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.setReturnType(XMLType.XSD_STRING);

			return (String) call.invoke(new Object[] { organizationId,
					departmentId });
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}

	public Boolean addGroups(String organizationId, String departmentId,
			String groupIds) {
		try {
			String method = "addGroups";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("departmentId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("groupIds", XMLType.XSD_STRING, ParameterMode.IN);
			call.setReturnType(XMLType.XSD_BOOLEAN);

			return (Boolean) call.invoke(new Object[] { organizationId,
					departmentId, groupIds });
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}

	public Boolean removeGroups(String organizationId, String departmentId,
			String groupIds) {
		try {
			String method = "removeGroups";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("departmentId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("groupIds", XMLType.XSD_STRING, ParameterMode.IN);
			call.setReturnType(XMLType.XSD_BOOLEAN);

			return (Boolean) call.invoke(new Object[] { organizationId,
					departmentId, groupIds });
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}

	public String allGroups(String organizationId, String departmentId) {
		try {
			String method = "allGroups";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("departmentId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.setReturnType(XMLType.XSD_STRING);

			return (String) call.invoke(new Object[] { organizationId,
					departmentId });
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}

	@Test
	public void testAdd() throws Exception {
		addDepartment(ORGANIZATIONID,
				WebServiceClient.readXml("department.xml"));
	}

	@Test
	public void testUpdate() throws Exception {
		updateDepartment(ORGANIZATIONID,
				WebServiceClient.readXml("department.xml"));
	}

	@Test
	public void testOther() throws Exception {
		String all = allDepartments(ORGANIZATIONID);
		System.out.println(all);

		String get = getDepartment(ORGANIZATIONID, "DE0001");
		System.out.println(get);

	}
	
	@Test
	public void testRelPe() throws Exception {
		addPersons(ORGANIZATIONID, "DE0001", "PE0001,PE0002");
		System.out.println(allPersons(ORGANIZATIONID,"DE0001"));
		
		removePersons(ORGANIZATIONID, "DE0001", "PE0001");
		System.out.println(allPersons(ORGANIZATIONID,"DE0001"));
	}
	
	@Test
	public void testRelPo() throws Exception {
		addPositions(ORGANIZATIONID, "DE0001", "PO0001");
		System.out.println(allPositions(ORGANIZATIONID,"DE0001"));
		
		removePositions(ORGANIZATIONID, "DE0001", "PO0001");
		System.out.println(allPositions(ORGANIZATIONID,"DE0001"));
	}
	
	@Test
	public void testRelGr() throws Exception {
		addGroups(ORGANIZATIONID, "DE0001", "GR0001,GR0002");
		System.out.println(allGroups(ORGANIZATIONID,"DE0001"));
		
		removeGroups(ORGANIZATIONID, "DE0001", "GR0001");
		System.out.println(allGroups(ORGANIZATIONID,"DE0001"));
	}
}
